package com.treasure.hunt.mine

import android.app.Activity
import android.content.Intent
import androidx.lifecycle.Observer
import com.module.lemlin.LoadingDialog
import com.module.lemlin.base.BaseAbstractActivity
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.treasure.hunt.R
import com.treasure.hunt.mode.OtherViewMode
import com.treasure.hunt.popup.DialogFeedPopup
import com.treasure.hunt.popup.DialogFlopPopup
import com.treasure.hunt.popup.DialogHintPopup
import kotlinx.android.synthetic.main.activity_mine_opinion.*


class MineOpinionActivity : ViewModeActivity<OtherViewMode>() {

    companion object {
        fun open(activity: Activity) {
            val intent = Intent(activity, MineOpinionActivity::class.java)
            activity.startActivity(intent)
        }
    }

    private var mOpinionType: Int = 0

    private val feedPopup: DialogFeedPopup by lazy {
        DialogFeedPopup(context = this) { _, type ->
            mOpinionType = type
            tvOpinionType.text = if (type == 1) "BUG反馈" else "产品建议"
        }
    }


    private val hintPopup: DialogHintPopup by lazy {
        DialogHintPopup(context = this,
            onConfirm = { view, type ->
                hintPopup.dismiss()
                finish()
            },
            onCancel = { view, type ->
                hintPopup.dismiss()
            })
    }

    override val layoutResId: Int
        get() = R.layout.activity_mine_opinion

    override fun viewModel(): OtherViewMode {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_020027,
            view = flMineOpinionBar
        )
    }

    override fun initView() {
        ivMineOpinionBack.setOnClickListener {
            finish()
        }
        tvOpinionType.setOnClickListener {
            feedPopup.showPopupWindow()
        }

        tvOpinionConfirm.setOnClickListener {
            if (mOpinionType == 0) {
                toast("请选择建议类型")
                return@setOnClickListener
            }
            val content: String = etOpinionContent.text.toString()
            if (content.isEmpty()) {
                toast("请输入反馈内容")
                return@setOnClickListener
            }
            viewModel.otherAddFeedback(content, mOpinionType)
        }
    }

    override fun initViewMode() {
        viewModel.baseLiveData.observe(this, Observer { response ->
            if (response.status == Status.START) {
                LoadingDialog.show(this, resId = R.layout.dialog_load_style_1)
            } else if (response.status == Status.FAILURE) {
                LoadingDialog.dismiss()
                toast(response.throwable?.msg ?: "未知错误")
            }
            if (response.status != Status.SUCCESS) {
                return@Observer
            }
            LoadingDialog.dismiss()
            hintPopup.setGoneTextCancel(true).setText(
                textTitle = "意见反馈",
                textMessage = "提交成功",
                textConfirm = "确认",
                textCancel = "取消"
            ).setOutSideDismiss(false).setBackPressEnable(false).showPopupWindow()
        })
    }


}